package net.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public abstract class UDPSever extends Thread
{
	private int bufferSize;
	protected DatagramSocket socket;
	public UDPSever(int port,int bufferSize) throws SocketException
	{
		this.bufferSize = bufferSize;
		this.socket = new DatagramSocket(port);
	}
	public UDPSever(int port) throws SocketException
	{
		this(port,8192);
	}
	@Override
	public void run()
	{
		byte[] buffer = new byte[bufferSize];
		while(true)
		{
			DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
			try
			{
				socket.receive(incoming);
				this.respond(incoming);
			}
			catch (Exception e)
			{
			}
		}
	}
	public abstract void respond(DatagramPacket request);
}
